<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot-scope="scope" slot="menuLeft">
          <!-- <el-button
            type="primary"
            size="small"
            v-permission="permissionList.submitBtn"
            @click.stop="handleBatchSubmit()"
            >提 交
          </el-button> -->
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.submitBtn"
            @click.stop="handleSubmit()"
            >清洗提交
          </el-button>
          <approve-button
            v-permission="permissionList.auditBtn"
            :approve-before="approveBefore"
            @approve="handleApprove"
            >清洗审批
          </approve-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.planApplyBtn"
            @click.stop="planApply()"
            >修改申请
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.planApproveBtn"
            @click.stop="planApprove()"
            >修改审批
          </el-button>
          <el-button type="primary" size="small" @click.stop="handleExport()"
            >导 出
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click.stop="handleInputDetail()"
            >入 库
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click.stop="handleOutputDetail()"
            >出 库
          </el-button>
          <el-button
            type="primary"
            size="small"
            :hidden="true"
            v-if="false"
            @click.stop="handlePrint()"
            >打 印
          </el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="scope.row.checkStatus == '0' || scope.row.checkStatus == '3'"
            v-permission="permissionList.editBtn"
            @click.stop="rowEdit(scope.row, scope.index)"
            >编 辑
          </el-button>
	  <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-permission="permissionList.delBtn"
            @click.stop="rowDelete(scope.row, scope.index)"
            >删 除
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="scope.row.checkApproveStatus=='3' && scope.row.checkStatus != '1' && scope.row.checkStatus != '2'"
            v-permission="permissionList.editPlanBtn"
            @click.stop="rowEditPlan(scope.row, scope.index)"
            >编辑日计划
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="
              (scope.row.personId == userCode ||scope.row.personTwoId == userCode) &&
              (scope.row.checkStatus == '0' || scope.row.checkStatus == '3')
            "
            v-permission="permissionList.completeBtn"
            @click.stop="rowCompleteInfo(scope.row, scope.index)"
            >完善信息
          </el-button>
        </template>
      </avue-crud>
      <open-dialog
        :DialogVisible.sync="addVisableDialogEditPlan"
        title="编辑日计划"
        width="70%"
      >
        <el-form>
          <avue-form v-model="formEditPlan" :option="tableOptionEditPlan"></avue-form>
          <div class="bottom-menu-plan-day">
            <el-button type="primary" @click="editPlan">保存</el-button>
            <el-button type="primary" @click="addVisableDialogEditPlan = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="addVisableDialog"
        title="编辑"
        width="70%"
      >
        <el-form>
          <avue-form v-model="formAdd" :option="tableOptionEdit"></avue-form>
          <div class="bottom-menu-plan-day">
            <el-button type="primary" @click="edit">保存</el-button>
            <el-button type="primary" @click="addVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="completeInfoVisableDialog"
        title="完善信息"
        width="70%"
      >
        <el-form>
          <avue-form
            v-model="formInfo"
            :option="tableOptionCompleteInfo"
          ></avue-form>
          <div class="bottom-menu-plan-day">
            <el-button type="primary" @click="completeInfo">保存</el-button>
            <el-button type="primary" @click="completeInfoVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import { getObj, putObj,delObj,submit,applyed,approved,inputDetailSubmit,outputDetailSubmit } from "@/api/subway/swstcleanplanday";
import {
  tableOptionEdit,
  tableOptionEditPlan,
  tableOptionCompleteInfo,nowYear,nowMonth
} from "@/const/crud/subway/swstcleanplanday";
import { dateFormat } from "@/util/date";
import store from "@/store";

export default {
  name: "StCleanPlanday",
  mixins: [baseCrud("subway/swstcleanplanday")],
  data() {
    return {
      exportName: "车票清洗周计划.xlsx",
      formAdd: [],
      formEditPlan: {},
      formInfo: [],
      addVisableDialog: false,
      addVisableDialogEditPlan: false,
      completeInfoVisableDialog: false,
      tableOptionEdit: tableOptionEdit,
      tableOptionEditPlan: tableOptionEditPlan,
      tableOptionCompleteInfo: tableOptionCompleteInfo,
      userCode: store.getters.userInfo.userCode,
      searchForm: {
        workTime: nowYear,
        planMonth: nowMonth
      }
    };
  },
  computed: {
    permissionList() {
      return {
        submitBtn: this.vaildData(
          this.permissions.st_plan_day_clean_submit,
          false
        ),
        editBtn: this.vaildData(this.permissions.st_plan_day_clean_edit, false),
	delBtn: this.vaildData(this.permissions.st_plan_day_clean_delete, false),
        completeBtn: this.vaildData(this.permissions.st_plan_day_clean_complete, false),
        auditBtn: this.vaildData(
          this.permissions.st_plan_day_clean_audit,
          false
        ),
        planApplyBtn: this.vaildData(this.permissions.st_plan_day_clean_plan_apply, false),
        planApproveBtn: this.vaildData(this.permissions.st_plan_day_clean_plan_approve, false)
      };
    },
  },
  watch: {
    formInfo: {
      handler(newValue, oldValue) {
        if (newValue.effectiveNumber || newValue.testNumber || newValue.invalidNumber) {
          newValue.totalNumber =
            newValue.effectiveNumber + newValue.testNumber + newValue.invalidNumber;
        }
      },
      immediate: true,
      deep: true,
    },
    formEditPlan: {
      handler(newValue, oldValue) {
        if (newValue.effectiveNumber || newValue.testNumber || newValue.invalidNumber) {
          newValue.totalNumber =
            newValue.effectiveNumber + newValue.testNumber + newValue.invalidNumber;
        }
      },
      immediate: true,
      deep: true,
    }
  },
  methods: {
    planApply: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.checkApproveStatus != "1" && table.checkApproveStatus != "4") {
          this.$message.error("请检查数据，只能选择日计划审批状态为未申请或审批未通过的数据进行申请！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        applyed(ids)
          .then(data => {
            this.$message.success("申请成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    planApprove: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.checkApproveStatus != "2") {
          this.$message.error("请检查数据，只能选择日计划审批状态为已申请的数据进行申请！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        approved(ids)
          .then(data => {
            this.$message.success("日计划审批成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    handleSubmit: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.checkStatus != "0" && table.checkStatus != "3") {
          this.$message.error("请检查数据，只能提交未提交的数据！");
          return false;
        }
	
       // if (table.actualFinishTime == null || (table.totalNumber == null || table.totalNumber == 0)) {
       if (table.actualFinishTime == null || (table.totalNumber == null)) {
          this.$message.error("请检查，选择的数据有未完善信息的，请完善信息！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        submit(ids)
          .then(data => {
            this.$message.success("提交成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    handleInputDetail:function(){
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if(table.isInstock=='1'){
	  this.$message.error("请检查选择的数据有已入库的数据，请重新选择！");
          return false;
	}
	if (this.validatenull(table.personName)) {
          this.$message.error("请检查选择的数据有未填写车票编码员的数据，请先填写车票编码员！");
          return false;
        }
        if (this.validatenull(table.actualFinishTime) || (this.validatenull(table.totalNumber)||table.totalNumber == 0)) {
          this.$message.error("请检查选择的数据有未完善信息的，请先完善信息！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认加入入库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        inputDetailSubmit(ids)
          .then(data => {
            this.$message.success("入库成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    handleOutputDetail:function(){
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if(table.isOutstock=='1'){
	  this.$message.error("请检查选择的数据有已出库的数据，请重新选择！");
          return false;
	}
        if (this.validatenull(table.personName)) {
          this.$message.error("请检查选择的数据有未填写车票编码员的数据，请先填写车票编码员！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认加入出库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        outputDetailSubmit(ids)
          .then(data => {
            this.$message.success("出库成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    rowEditPlan: function (row) {
      row.$cellEdit = true;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formEditPlan = res;
          // let now = new Date();
          // if (now.getHours() < 6) {
          //   now.setDate(now.getDate() - 1);
          // }
          // this.formEditPlan.actualFinishTime = dateFormat(now);
        })
        .then(() => {
          this.addVisableDialogEditPlan = true;
        });
    },
    editPlan: function () {
      putObj(this.formEditPlan)
        .then((data) => {
          this.$message.success("修改成功");
          this.addVisableDialogEditPlan = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
      //this.formAdd.actualFinishTime = dateF;
    },
    rowEdit: function (row) {
      row.$cellEdit = true;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formAdd = res;
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate(now.getDate() - 1);
          }
          this.formAdd.actualFinishTime = dateFormat(now);
        })
        .then(() => {
          this.addVisableDialog = true;
        });
    },
    rowDelete: function (row) {
      this.$confirm("确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delObj(row)
            .then((data) => {
              this.$message.success("删除成功");
              this.getList(this.page);
              done();
            })
            .catch(() => {
              loading();
            });
        });
    },
    edit: function () {
      putObj(this.formAdd)
        .then((data) => {
          this.$message.success("修改成功");
          this.addVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
      //this.formAdd.actualFinishTime = dateF;
    },
    rowCompleteInfo: function (row) {
      row.$cellEdit = true;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formInfo = res;
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate(now.getDate() - 1);
          }
          this.formInfo.actualFinishTime = dateFormat(now);
        })
        .then(() => {
          this.completeInfoVisableDialog = true;
        });
    },
    completeInfo: function () {
      putObj(this.formInfo)
        .then((data) => {
          this.$message.success("完善信息成功");
          this.completeInfoVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    approveBefore(callback) {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审批的记录");
        return;
      }
      for (let row of tableSelect) {
        if (row.checkStatus != "1") {
          this.$message.info(
            `序号为${row.$index + 1}的记录不是已提交状态不能审批`
          );
          return;
        }
      }
      callback();
    },
  },
};
</script>
<style lang="scss" scoped>
.bottom-menu-plan-day {
  text-align: center;
}
</style>

